package com.grain.platform.listener;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.model.Url;
import com.bstek.bdf2.core.view.frame.main.MainFrame;
import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.base.CardBook;
import com.bstek.dorado.view.widget.base.accordion.Accordion;
import com.bstek.dorado.view.widget.base.accordion.Section;
import com.bstek.dorado.view.widget.base.menu.Menu;
import com.bstek.dorado.view.widget.base.menu.MenuItem;
import com.bstek.dorado.view.widget.base.toolbar.Button;
import com.bstek.dorado.view.widget.base.toolbar.Separator;
import com.bstek.dorado.view.widget.base.toolbar.ToolBar;
import com.bstek.dorado.view.widget.blockview.BlockView;

/**
 * 首页菜单控制
 * 
 * @author Andy
 *
 */
@Component(IndexListener.BEAN_ID)
public class IndexListener {

	public static final String BEAN_ID = "listener.index";

	@Autowired
	private MainFrame mainFrame;

	/**
	 * 主界面拦截，重新世界主界面的菜单显示效果 spring:listener.mainFrame#onViewInit
	 * 
	 * @param view
	 * @throws Exception
	 */
	public void onViewInit(View view) throws Exception {
		Collection<Url> result = mainFrame.loadMeunUrls(null);

		if (null == result || result.isEmpty()) return;

		ToolBar toolBarNav = (ToolBar) view.getViewElement("toolBarNav");
		CardBook cardBookNav = (CardBook) view.getViewElement("cardBookNav");
		Menu menuIndex =  (Menu) view.getViewElement("menuIndex");
		Button btn = null;
		MenuItem item  = null;
		int count = 0;
		for (Url url : result) {
			
			//使用按钮样式
			btn = new Button();
			btn.setCaption(url.getName());
			btn.setTip(url.getDesc());
			//btn.setIcon(url.getIcon());
			btn.setIconClass(url.getIcon());
			btn.setTags("mainMenuBtn");
			btn.setUserData(count);

			// buildBlockView(url,cardBookNav);
			buildSections(url, cardBookNav);
			
			toolBarNav.addItem(btn);
			toolBarNav.addItem(new Separator());
			
			
			//使用菜单样式
			item = new MenuItem();
			item.setCaption(url.getName());
			item.setIconClass(url.getIcon());
			item.setTags("mainMenuItem");
			item.setUserData(count);
			menuIndex.addItem(item);
			
			
			if (count == 0) {
				btn.setToggled(true);
				cardBookNav.setCurrentIndex(0);
			}
			
			count++;
			
		}
	}

	/**
	 * 直接使用BlockView，将子菜单放到userData中
	 * 
	 * @param url
	 * @param cardBookNav
	 */
	// private void buildBlockView(Url url,CardBook cardBookNav) {
	// String id = url.getId();
	// String newId = "blockView"+id.replaceAll("-","");
	// List<Url> cUrl = url.getChildren();
	// if(null == cUrl || cUrl.isEmpty()) return;
	// BlockView blockView = new BlockView();
	// blockView.setId("blockViewId");
	// blockView.setItems(cUrl);
	//
	// Map<String, Object> style = new HashMap<String, Object>();
	// style.put("padding-left", "10px");
	// blockView.setTags("mainMenuBlock");
	// blockView.setStyle(style);
	//
	// cardBookNav.addControl(blockView);
	// }
	//
	//

	/**
	 * 手风琴下带BlockView
	 * 
	 * @param url
	 * @param cardBookNav
	 */
	private void buildSections(Url url, CardBook cardBookNav) {
		String id = url.getId();
		String newId = "accordion" + id.replaceAll("-", "");

		Accordion newAccordion = new Accordion();
		newAccordion.setId(newId);

		List<Url> cUrl = url.getChildren();
		if (null == cUrl || cUrl.isEmpty())
			return;

		Section section = null;
		BlockView blockView = null;
		Map<String, Object> style = new HashMap<String, Object>();
		style.put("padding-left", "10px");
		for (Url url2 : cUrl) {
			section = new Section();
			blockView = new BlockView();
			blockView.setBlockHeight(60);
			blockView.setBlockWidth(140);
			blockView.setTags("mainMenuBlock");
			blockView.setStyle(style);

			section.setCaption(url2.getName());
			section.setTip(url2.getDesc());
			//section.setIcon(url2.getIcon());
			section.setIconClass(url2.getIcon());

			section.setName(url2.getId());
			section.setUserData(url2);

			blockView.setItems(url2.getChildren());
			section.setControl(blockView);

			newAccordion.addSection(section);
		}
		cardBookNav.addControl(newAccordion);
	}

}
